<?php
defined ( 'BASEPATH' ) or die ( 'forbidden' );
class gameitems_Controller extends Controller {
    //private $type;
    //private $name;
    //private $begintime;
    //private $endtime;
    //private $tmpcontent;
    //private $content;
    //private $tmpsequence;
    private $areaList;    //区号列表
    private $wholeServerHost;    //全局地址

    function _init() {
        //$sql = "select COUNT(id) AS num from tt_notice";
        //$one = $this->db->one($sql);
        //for ($i = 1; $i <= $one['num']; $i++)
        //{
        //    $this->tmpsequence[$i] = $i;
        //}
        $config = get_config();
        $this->wholeServerHost = $config['host_url'];
        $server_list=get_server_list();
        foreach($server_list as $k=>$row) {
            $this->areaList[$row['SvrOutAddr']] = $row['SvrID']."-".$row['SvrName'];
        }
    }
    function index() {
        $this->view->display ( "gameitems/index" );
    }
    function flexigrid() {
        list ( $colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page ) = $this->common->get_flexigrid_params ();

        $sql = "SELECT COUNT(id) AS num FROM type_object WHERE {$where}";
        $one = $this->db->one ( $sql );
        $total = $one ['num'];
        $orderby = $sortname ? "ORDER BY {$sortname} {$sortorder} " : "";
        $sql = "SELECT {$colsinfo} FROM type_object WHERE {$where}";
        $this->db->query ( $sql );
        while ( $n = $this->db->get_array () ) {
            $row [] = array (
                    'id' => $n ['id'],
                    'cell' => $n 
            );
        }
        $result = array (
                'page' => $page,
                'total' => $total,
                'rows' => $row 
        );
        echo json_encode ( $result );
    }

    function add() {
        if (is_submit ()) {
            $rowdata = $_POST ['row'];

            var_dump($rowdata);

            $result = $this->db->insert ( 'type_object', $rowdata);
        }

        $this->view->display ( "gameitems/add" );
    }

    function edit($id = 0) {
        $arr = array();
        $sql = "select id, name, kind, subkind, color, stacklimit, buygold, sellgold, candecompose, recommend from type_object where id=$id";
        $row = $this->db->one($sql);
        unset($arr);
        foreach ($row as $key => $value) {
            $arr[$key] = $value;
        }

        if (is_submit()) {
            $rowdata = $_POST['row'];
            $result = $this->db->update('type_object', $rowdata, array('id' => $id));
            if (!$result) {
                echo "is failed to update game item info";            
            }
        }
        
        $this->view->assign('id', $arr['id']);
        $this->view->assign('name', $arr['name']);
        $this->view->assign('kind', $arr['kind']);
        $this->view->assign('subkind', $arr['subkind']);
        $this->view->assign('color', $arr['color']);
        $this->view->assign('stacklimit', $arr['stacklimit']);
        $this->view->assign('buygold', $arr['buygold']);
        $this->view->assign('sellgold', $arr['sellgold']);
        $this->view->assign('candecompose', $arr['candecompose']);
        $this->view->assign('recommend', $arr['recommend']);

        $this->view->display("gameitems/edit");
    }

    function del($id = 0) {
	echo "1111111111111111111";
        $c = 0;
        $r = '';
        
        $where = array('id' => $id);
        if ($this->db->delete('type_object', $where)) {
            insert_log(translate('MENU_GAME_ITEMS'),translate('DEL'), array('id'=>$id));
            $c = 1;
            $r = translate('C_TIPS_SUCCESS_TEXT');
            $config = get_config();
        } else {
            $r = translate('C_TIPS_ERROR_TEXT');
        }
        json_output($c, $r);
    }
    
    function multi() {
        $type = $this->input->post("type");
        $ids = $this->input->post("ids");
        $c = 0;
        $r = '';
        if (count(explode(',', $ids)) == 0) {
            $r = translate('C_INVALID_PARAMS');
        } else {
            if ($type == 'delete') {
                $this->db->query("DELETE FROM tt_notice WHERE id IN ({$ids})");
                insert_log(translate('MENU_NOTICE_NEWMANAGER'),translate('MULTI_DEL'),array('ids'=> $ids));
                $c = 1;
                $config = get_config();
                $URL_NOTICE = $config['host_url_backstage'] . "master/duoluotaitanGM/api/sendNoticeInfo";
                #var_dump($URL_NOTICE);
                #exit(0);
                $value = file_get_contents($URL_NOTICE);
            } else {
                $r = translate('C_INVALID_PARAMS');
            }
        }
        json_output($c, $r);
    }
}




?>

